﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Revit
{
	public static class HYUpdateLinkedDocXML
	{
		public static bool AddRootAndFileListNodes(string XMLPath, MajorSystemType type, List<string> fileList)
		{
			bool result = false;
			try
			{
				XmlDocument xmlDocument = XMLOperating.LoadXml(XMLPath, HYUpdateLinkedDocXML.xmlRootNode);
				XmlNode xmlNode = xmlDocument.SelectSingleNode(HYUpdateLinkedDocXML.xmlRootNode);
				XmlElement xmlElement = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlMajorSystemType);
				xmlNode.AppendChild(xmlElement);
				XMLOperating.AddAttribute(xmlElement, "Type", type.ToString());
				XmlElement xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlFileListNode);
				xmlElement.AppendChild(xmlElement2);
				foreach (string attValue in fileList)
				{
					XmlElement xmlElement3 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlFileNameName);
					if (xmlElement3 != null)
					{
						XMLOperating.AddAttribute(xmlElement3, "Name", attValue);
						xmlElement2.AppendChild(xmlElement3);
					}
				}
				result = XMLOperating.SaveXml(xmlDocument, XMLPath);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
				throw ex;
			}
			return result;
		}

		public static bool AddElementListToXML(string XMLPath, string fatherNode, List<HYUpdateElementInfo> nodeData)
		{
			bool result = false;
			try
			{
				XmlDocument xmlDocument = XMLOperating.LoadXml(XMLPath);
				XmlNode xmlNode = xmlDocument.SelectSingleNode(HYUpdateLinkedDocXML.xmlRootNode);
				if (xmlNode != null)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode(HYUpdateLinkedDocXML.xmlMajorSystemType);
					if (xmlNode2 != null)
					{
						XmlNode xmlNode3 = xmlNode2.SelectSingleNode(HYUpdateLinkedDocXML.xmlFileListNode);
						if (xmlNode3 != null)
						{
							XmlNodeList xmlNodeList = xmlNode3.SelectNodes(HYUpdateLinkedDocXML.xmlFileNameName);
							XmlNode xmlNode4 = null;
							foreach (object obj in xmlNodeList)
							{
								XmlNode xmlNode5 = (XmlNode)obj;
								XmlElement xmlElement = xmlNode5 as XmlElement;
								if (xmlElement != null && !string.IsNullOrEmpty(xmlElement.GetAttribute("Name")) && fatherNode == xmlElement.GetAttribute("Name"))
								{
									xmlNode4 = xmlNode5;
								}
							}
							if (xmlNode4 != null)
							{
								foreach (HYUpdateElementInfo hyupdateElementInfo in nodeData)
								{
									XmlElement xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlElementInfo);
									XmlNode xmlNode6 = xmlNode4.AppendChild(xmlElement2);
									HYUpdateLinkedDocXML.WriteNodeAttribute(xmlElement2, hyupdateElementInfo);
									HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = hyupdateElementInfo as HYUpdateAttachedEleInfo;
									if (hyupdateAttachedEleInfo != null)
									{
										if (hyupdateAttachedEleInfo.InElementInfo != null && hyupdateAttachedEleInfo.InElementInfo.ElementID != -2147483648)
										{
											xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlInElementInfo);
											xmlNode6.AppendChild(xmlElement2);
											HYUpdateLinkedDocXML.WriteNodeAttribute(xmlElement2, hyupdateAttachedEleInfo.InElementInfo);
										}
										if (hyupdateAttachedEleInfo.OutElementInfo != null && hyupdateAttachedEleInfo.OutElementInfo.ElementID != -2147483648)
										{
											xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlOutElementInfo);
											xmlNode6.AppendChild(xmlElement2);
											HYUpdateLinkedDocXML.WriteNodeAttribute(xmlElement2, hyupdateAttachedEleInfo.OutElementInfo);
										}
										if (hyupdateAttachedEleInfo.SideElementInfo != null && hyupdateAttachedEleInfo.SideElementInfo.ElementID != -2147483648)
										{
											xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlSideElementInfo);
											xmlNode6.AppendChild(xmlElement2);
											HYUpdateLinkedDocXML.WriteNodeAttribute(xmlElement2, hyupdateAttachedEleInfo.SideElementInfo);
										}
										if (hyupdateAttachedEleInfo.Side2ElementInfo != null && hyupdateAttachedEleInfo.Side2ElementInfo.ElementID != -2147483648)
										{
											xmlElement2 = xmlDocument.CreateElement(HYUpdateLinkedDocXML.xmlSide2ElementInfo);
											xmlNode6.AppendChild(xmlElement2);
											HYUpdateLinkedDocXML.WriteNodeAttribute(xmlElement2, hyupdateAttachedEleInfo.Side2ElementInfo);
										}
									}
								}
								result = XMLOperating.SaveXml(xmlDocument, XMLPath);
							}
							else
							{
								YJKMessageBox.Error(string.Format("没有找到节点：{0}", HYUpdateLinkedDocXML.xmlFileNameName + " Name=" + fatherNode));
							}
						}
					}
					else
					{
						YJKMessageBox.Error(string.Format("没有找到节点：{0}", HYUpdateLinkedDocXML.xmlMajorSystemType));
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
				throw ex;
			}
			return result;
		}

		public static List<HYUpdateElementInfo> LoadXMLToElementList(string XMLPath, string fatherNode, ref MajorSystemType type)
		{
			List<HYUpdateElementInfo> list = new List<HYUpdateElementInfo>();
			try
			{
				XmlDocument xmlDocument = XMLOperating.LoadXml(XMLPath);
				XmlNode xmlNode = xmlDocument.SelectSingleNode(HYUpdateLinkedDocXML.xmlRootNode);
				if (xmlNode != null)
				{
					XmlNode xmlNode2 = xmlNode.SelectSingleNode(HYUpdateLinkedDocXML.xmlMajorSystemType);
					if (xmlNode2 != null)
					{
						XmlElement xmlElement = xmlNode2 as XmlElement;
						if (xmlElement != null && !string.IsNullOrEmpty(xmlElement.GetAttribute("Type")))
						{
							type = (MajorSystemType)Enum.Parse(typeof(MajorSystemType), xmlElement.GetAttribute("Type"));
						}
						XmlNode xmlNode3 = xmlNode2.SelectSingleNode(HYUpdateLinkedDocXML.xmlFileListNode);
						if (xmlNode3 != null)
						{
							XmlNodeList xmlNodeList = xmlNode3.SelectNodes(HYUpdateLinkedDocXML.xmlFileNameName);
							XmlNode xmlNode4 = null;
							foreach (object obj in xmlNodeList)
							{
								XmlNode xmlNode5 = (XmlNode)obj;
								XmlElement xmlElement2 = xmlNode5 as XmlElement;
								if (xmlElement2 != null && !string.IsNullOrEmpty(xmlElement2.GetAttribute("Name")) && fatherNode == xmlElement2.GetAttribute("Name"))
								{
									xmlNode4 = xmlNode5;
								}
							}
							if (xmlNode4 != null)
							{
								new List<XmlNode>();
								IEnumerator enumerator2 = xmlNode4.ChildNodes.GetEnumerator();
								{
									while (enumerator2.MoveNext())
									{
										object obj2 = enumerator2.Current;
										XmlNode xmlNode6 = (XmlNode)obj2;
										HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = new HYUpdateAttachedEleInfo();
										XmlElement xmlElement3 = xmlNode6 as XmlElement;
										if (xmlElement3 != null)
										{
											HYUpdateLinkedDocXML.ReadNodeAttribute(xmlElement3, hyupdateAttachedEleInfo);
										}
										foreach (object obj3 in xmlNode6.ChildNodes)
										{
											XmlNode xmlNode7 = (XmlNode)obj3;
											XmlElement xmlElement4 = xmlNode7 as XmlElement;
											if (xmlElement4 != null)
											{
												if (xmlElement4.Name == HYUpdateLinkedDocXML.xmlInElementInfo)
												{
													hyupdateAttachedEleInfo.InElementInfo = new HYUpdateElementInfo();
													HYUpdateLinkedDocXML.ReadNodeAttribute(xmlElement4, hyupdateAttachedEleInfo.InElementInfo);
												}
												else if (xmlElement4.Name == HYUpdateLinkedDocXML.xmlOutElementInfo)
												{
													hyupdateAttachedEleInfo.OutElementInfo = new HYUpdateElementInfo();
													HYUpdateLinkedDocXML.ReadNodeAttribute(xmlElement4, hyupdateAttachedEleInfo.OutElementInfo);
												}
												else if (xmlElement4.Name == HYUpdateLinkedDocXML.xmlSideElementInfo)
												{
													hyupdateAttachedEleInfo.SideElementInfo = new HYUpdateElementInfo();
													HYUpdateLinkedDocXML.ReadNodeAttribute(xmlElement4, hyupdateAttachedEleInfo.SideElementInfo);
												}
												else if (xmlElement4.Name == HYUpdateLinkedDocXML.xmlSide2ElementInfo)
												{
													hyupdateAttachedEleInfo.Side2ElementInfo = new HYUpdateElementInfo();
													HYUpdateLinkedDocXML.ReadNodeAttribute(xmlElement4, hyupdateAttachedEleInfo.Side2ElementInfo);
												}
											}
										}
										list.Add(hyupdateAttachedEleInfo);
									}
									goto IL_2C6;
								}
							}
							YJKMessageBox.Error(string.Format("没有找到节点：{0}", HYUpdateLinkedDocXML.xmlFileNameName + " Name=" + fatherNode));
						}
					}
					else
					{
						YJKMessageBox.Error(string.Format("没有找到节点：{0}", HYUpdateLinkedDocXML.xmlMajorSystemType));
					}
				}
				IL_2C6:;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
				throw ex;
			}
			return list;
		}

		public static void WriteNodeAttribute(XmlElement xmlElem, HYUpdateElementInfo elemInfo)
		{
			if (xmlElem != null && elemInfo != null)
			{
				XMLOperating.AddAttribute(xmlElem, "Document", elemInfo.DocumentTitle);
				XMLOperating.AddAttribute(xmlElem, "ID", elemInfo.ElementID.ToString());
				XMLOperating.AddAttribute(xmlElem, "CategoryName", elemInfo.CategoryName);
				XMLOperating.AddAttribute(xmlElem, "PipeDN", elemInfo.PipeDN.ToString());
				XMLOperating.AddAttribute(xmlElem, "Width", elemInfo.Width.ToString());
				XMLOperating.AddAttribute(xmlElem, "Height", elemInfo.Height.ToString());
				XMLOperating.AddAttribute(xmlElem, "FLow", elemInfo.FLow.ToString("f2"));
				XMLOperating.AddAttribute(xmlElem, "Velocity", elemInfo.Velocity.ToString("f2"));
				XMLOperating.AddAttribute(xmlElem, "Diameter", elemInfo.Diameter.ToString("f2"));
				XMLOperating.AddAttribute(xmlElem, "Shape", elemInfo.Shape.ToString());
				XMLOperating.AddAttribute(xmlElem, "Horizontal", elemInfo.Horizontal.ToString());
			}
		}

		public static void ReadNodeAttribute(XmlElement xmlElem, HYUpdateElementInfo elemInfo)
		{
			if (xmlElem != null && elemInfo != null)
			{
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Document")))
				{
					elemInfo.DocumentTitle = xmlElem.GetAttribute("Document");
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("ID")))
				{
					elemInfo.ElementID = Convert.ToInt32(xmlElem.GetAttribute("ID"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("CategoryName")))
				{
					elemInfo.CategoryName = xmlElem.GetAttribute("CategoryName");
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("PipeDN")))
				{
					elemInfo.PipeDN = Convert.ToInt32(xmlElem.GetAttribute("PipeDN"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Width")))
				{
					elemInfo.Width = Convert.ToInt32(xmlElem.GetAttribute("Width"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Height")))
				{
					elemInfo.Height = Convert.ToInt32(xmlElem.GetAttribute("Height"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("FLow")))
				{
					elemInfo.FLow = Convert.ToDouble(xmlElem.GetAttribute("FLow"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Velocity")))
				{
					elemInfo.Velocity = Convert.ToDouble(xmlElem.GetAttribute("Velocity"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Diameter")))
				{
					elemInfo.Diameter = Convert.ToDouble(xmlElem.GetAttribute("Diameter"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Shape")))
				{
					elemInfo.Shape = (HYUpdateElementShape)Enum.Parse(typeof(HYUpdateElementShape), xmlElem.GetAttribute("Shape"));
				}
				if (!string.IsNullOrEmpty(xmlElem.GetAttribute("Horizontal")))
				{
					elemInfo.Horizontal = Convert.ToBoolean(xmlElem.GetAttribute("Horizontal"));
				}
			}
		}

		public static readonly string xmlRootNode = "ElementInfoList";

		public static readonly string xmlFileListNode = "LinkedFilesList";

		public static readonly string xmlFileNameName = "LinkedFile";

		public static readonly string xmlElementInfo = "ElementInfo";

		public static readonly string xmlInElementInfo = "InElementInfo";

		public static readonly string xmlOutElementInfo = "OutElementInfo";

		public static readonly string xmlSideElementInfo = "SideElementInfo";

		public static readonly string xmlSide2ElementInfo = "Side2ElementInfo";

		public static readonly string xmlMajorSystemType = "MajorSystemType";
	}
}
